#Tk创建一个界面
import threading
from tkinter import *
from tkinter import scrolledtext
from tkinter.font import Font
from tkinter.ttk import *
import zipfile
from tkinter import filedialog
import os
import shutil
import xml.dom.minidom

home = os.getcwd()
# 动态打印的变量
LOG_LINE_NUM = 0

class Gui(Frame):
    # 这个类仅实现界面生成功能，具体事件处理代码在子类Application中。
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('作业复制')
        self.master.geometry('500x180')
        self.master.resizable(0, 0)  # 禁止拉伸
        self.gui()

    def gui(self):
        self.text = Label(self.master, text='输出框', font=('微软雅黑', 9))
        self.text.place(x=120, y=10, width=350, height=30)
        self.sep1 = Separator(self.master, orient=HORIZONTAL)
        self.sep1.pack(padx=10, pady=40, fill='x')
        #写一个输出框
        self.log_data_Text = scrolledtext.ScrolledText(self.master, width=50, height=5, font=('微软雅黑', 10))
        self.log_data_Text.place(x=10, y=50, width=300, height=120)
        #写一个按钮
        self.button = Button(self.master, text='数据结构',command=self.SelectPath)
        self.button.place(x=310, y=50, width=180, height=55)
        self.button = Button(self.master, text='Android',command=self.Android)
        self.button.place(x=310, y=115, width=180, height=55)
        
    def selectPath(self):
        #选择文件path_接收文件地址
        path_i = filedialog.askopenfilename(filetypes=[("java", "*.java")])
        #获取path_i的文件名
        path_i_name = os.path.basename(path_i)
        #打开选择的文件
        dom = xml.dom.minidom.parse('name.xml')
        logins=dom.getElementsByTagName("log")  #获取节点列表
        for i in range(len(logins)):  #获取节点属性
            names=logins[i].getAttribute("name")
            xuehao=logins[i].getAttribute("xuehao")
            douhao=','.join(xuehao)
            #复制path_i并重命名为names
            names=names+xuehao
            shutil.copyfile(path_i,names+'.java')
            path_= names+'.java'
            file= path_
            old_str="2,0,2,1,0,3,5,1,0,7,1,1,8"
            new_str=douhao
            file_data = ""
            with open(file, "r", encoding="utf-8") as f:
                for line in f:
                    if old_str in line:
                        line = line.replace(old_str,new_str)
                    file_data += line
            with open(file,"w",encoding="utf-8") as f:
                f.write(file_data)
            #移动文件到桌面
            desktop_path = os.path.join(os.path.expanduser('~'),"Desktop/")
            #在desktop_path里创建out文件夹
            if not os.path.exists(desktop_path+"/out"):
                os.makedirs(desktop_path+"/out")
            # #复制文件到out文件夹
            shutil.copyfile(path_,desktop_path+"/out/"+path_i_name)
            homes=desktop_path+"/out/"
            #压缩homes下的文件，排除zip文件
            z = zipfile.ZipFile(names+'.zip','w',zipfile.ZIP_DEFLATED)
            for dirpath, dirnames, filenames in os.walk(homes):
                fpath = dirpath.replace(homes,'')
                fpath = fpath and fpath + os.sep or ''
                for filename in filenames:
                    if filename[-4:] != '.zip':
                        z.write(os.path.join(dirpath, filename),fpath+filename)
            z.close()
            #将压缩文件移动到homes下
            shutil.move(names+'.zip',homes)
            #删除文件
            os.remove(path_)
            os.remove(desktop_path+"/out/"+path_i_name)
            
            self.log_data_Text.insert(END, names+'.zip'+'复制完成'+'\n')

    def android(self):
        path_i = filedialog.askopenfilename(filetypes=[("zip", "*.zip")])
        dom = xml.dom.minidom.parse('name.xml')
        logins=dom.getElementsByTagName("log")  #获取节点列表
        for i in range(len(logins)):  #获取节点属性
            names=logins[i].getAttribute("name")
            names+=logins[i].getAttribute("xuehao")
            desktop_path = os.path.join(os.path.expanduser('~'),"Desktop/")
            #在desktop_path里创建out文件夹
            if not os.path.exists(desktop_path+"/out"):
                os.makedirs(desktop_path+"/out")
            #复制文件到out文件夹
            shutil.copyfile(path_i,desktop_path+"/out/"+names+'.zip')
            #打印
            self.log_data_Text.insert(END, names+'.zip'+'复制完成'+'\n')

    def SelectPath(self):
        self.thread_it(self.selectPath)
    def Android(self):
        self.thread_it(self.android)

    #开线程函数
    def thread_it(self, func, *args):
        '''将函数打包进线程'''
        # 创建
        t = threading.Thread(target=func, args=args)
        # 守护 !!!
        t.setDaemon(True)
        # 启动
        t.start()
#主函数
if __name__ == "__main__":
    top = Tk()
    Gui(top).mainloop()